package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.model.Employee;
import com.clqteam.model.Product;
import com.clqteam.service.insertZsService;
import com.clqteam.serviceimpl.insertZsServiceImpl;

public class insertZsController extends HttpServlet{
	
	insertZsService ins = new insertZsServiceImpl();
	      @Override
	    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    	  doPost(req, resp);
	    }
	      
	      
	      @Override
	    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    	//获取系统时间
          	 Date date=new Date();
          	 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          	 String drTime=format.format(date);    
          	 
          	 //自损信息录入
        		String zspIds=req.getParameter("zspId");
        		int zspId  = Integer.parseInt(zspIds);
        		Product p = ins.getProductInfoByPid(zspId);
        		String zspName=p.getpName();
        		String zspAmounts = req.getParameter("zspAmount");
        		int zspAmount  = Integer.parseInt(zspAmounts);
        		double zspCost  = p.getpCost();
        		String zsTime = req.getParameter("zsTime");
        		int ss = p.getpAmount();
        		int n = ss-zspAmount;
        		String zsReason=req.getParameter("zsReason");
        		// 获取session 信息
        		 HttpSession session = req.getSession();
            	 Employee emp = (Employee) session.getAttribute("employee");
             	 String eName = emp.geteName();
             	 int eId = emp.geteId();
        		
        		if (ss>zspAmount) {
        			int m = ins.insertZsByAllInfo(zspId, zspName, zspAmount, zspCost, drTime, eName, zsReason);
            		int k = ins.updateShByZs(zspId, n);
        			String mm = Integer.toString(m);
              		PrintWriter pw = resp.getWriter();
              		pw.write(mm);
				}else {
					String mm = "0";
              		PrintWriter pw = resp.getWriter();
              		pw.write(mm);
				}
      	//写入日志
         	 StringBuffer drInfos = new StringBuffer();
        		drInfos.append("用户");
        		drInfos.append(eName);
        		drInfos.append("新增商品自损,商品编号为");
        		drInfos.append(zspId);
        		drInfos.append("自损数量为");
        		drInfos.append(zspAmount);
        		String drInfo = drInfos.toString();
               ins.setdirInfo(drInfo, drTime, eId, eName);
       //写入订单修改
               int sn =ins.getProductInfoByPid(zspId).getpAmount();
               int sm = sn-zspAmount;
               double oMoney =sm*zspCost;
               int oId = ins.getOidByPid(zspId);
               ins.updataOrder(oId, oMoney);
               ins.updataOrderInfo(oId, sm);
               
               
	    	  
	    	  
	    	  
	    	  
	    	  
	    	  
	    	  
	    }
	             

}
